﻿using MCUPackage;
using MCUPackage.DesignEditor;
using MCUPackage.Utils;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;

namespace SxPackages.STC32G.Models
{
	[Serializable]
	class AllUartOptions
	{
		/// <summary>
		/// 串口配置结合
		/// </summary>
		[Browsable(false),
		JsonIgnore]
		public USARTOptionBase[] All { get; private set; }

		[Category("串口配置"),
		DisplayName("串口1"),
		TypeConverter(typeof(PropertySorter))]
		public USART1Options USART1 { get; private set; }

		[Category("串口配置"),
		DisplayName("串口2"),
		TypeConverter(typeof(PropertySorter))]
		public USART2Options USART2 { get; private set; }

		[Category("串口配置"),
		DisplayName("串口3"),
		TypeConverter(typeof(PropertySorter))]
		public UART3Options UART3 { get; private set; }

		[Category("串口配置"),
		DisplayName("串口4"),
		TypeConverter(typeof(PropertySorter))]
		public UART4Options UART4 { get; private set; }

		public AllUartOptions(STC32GQFP64 aParent)
		{
			this.USART1 = new USART1Options(aParent);
			this.USART2 = new USART2Options(aParent);
			this.UART3 = new UART3Options(aParent);
			this.UART4 = new UART4Options(aParent);
			All = new USARTOptionBase[] { this.USART1, this.USART2, this.UART3, this.UART4 };
		}

		/// <summary>
		/// 是否有启用的串口
		/// </summary>
		/// <returns></returns>
		public bool HasEnabled()
		{
			foreach (var uart in All)
			{
				if (uart.Enable)
				{
					return true;
				}
			}

			return false;
		}

		/// <summary>
		/// 是否有串口开启中断
		/// </summary>
		/// <returns></returns>
		public bool HasInterrupt()
		{
			foreach (var uart in All)
			{
				if (uart.Enable && uart.EnableTI)
				{
					return true;
				}
			}

			return false;
		}

		/// <summary>
		/// 是否有启用的DMA
		/// </summary>
		/// <returns></returns>
		public bool HasDMA()
		{
			foreach (var uart in All)
			{
				if (uart.Enable && (uart.EnableDMARcv || uart.EnableDMASnd))
				{
					return true;
				}
			}

			return false;
		}

		public bool AppendConfig(List<string[]> aLst)
		{
			if (!HasEnabled())
			{
				return false;
			}

			string name = "UART_Config";
			StringBuilder funcSb = new StringBuilder();
			funcSb.Append(TemplateHelper.GetFunctionCommit("串口模块初始化"));
			funcSb.AppendLine($"PRIVATE void {name}(void)").AppendLine("{");
			funcSb.AppendLine("\tCOMx_InitDefine attr;");
			if (this.HasDMA())
			{
				funcSb.AppendLine("\tDMA_UART_InitTypeDef dma;");
			}

			Dictionary<string, List<string>> lst = new Dictionary<string, List<string>>();
			StringBuilder varSb = new StringBuilder();
			StringBuilder tmpSb = new StringBuilder();

			foreach (var uart in this.All)
			{
				var strs = uart.Generate(lst);
				if (strs != null)
				{
					tmpSb.AppendLine();
					tmpSb.Append(strs[0]);
					if (!string.IsNullOrEmpty(strs[1]))
					{
						varSb.Append(strs[1]);
					}
				}
			}

			if (lst.Count > 0)
			{
				funcSb.AppendLine();
				funcSb.Append(GPIOOptions.GetGPIOConfigs(lst));
			}

			funcSb.Append(tmpSb);
			funcSb.AppendLine("}");

			string str = varSb.ToString();
			if (!string.IsNullOrEmpty(str))
			{
				str = "// 收发共用缓存，同时使用多路串口时每个串口需分别定义缓存，以免相互干扰\r\n" + str;
			}

			aLst.Add(new string[] { funcSb.ToString(), $"{name}();", str });

			return true;
		}
	}
}
